/*
 * Copyright (C) 2012 CNL - ECE Dept - National University of Singapore
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package sg.nus.ece.cnl.veracity.hadoop.generator;

/**
 *
 * @author tram
 */
public class ConfigurationGenerator {

    private static ConfigurationGenerator vmConfGeneratorInstance;

    private ConfigurationGenerator() {
    }

    public static ConfigurationGenerator getInstance() {

        if (vmConfGeneratorInstance == null) {
            vmConfGeneratorInstance = new ConfigurationGenerator();
        }

        return vmConfGeneratorInstance;
    }

    /**
     * Render a string containing the contents of the core-site.xml file with
     * master node address
     *
     * @param masterNode master node name or IP address
     * @return a string containing result value
     * @throws java.lang.Exception
     */
    public String getCoreSite(String masterNode) throws java.lang.Exception {

        VelocityUtil velocity = new VelocityUtil("sg/nus/ece/cnl/veracity/hadoop/velocity/core-site.vm");

        velocity.put("masternode", masterNode);

        return velocity.merge().toString();
    }

    /**
     * Render a string containing the contents of the mapred-site.xml file with
     * master node address
     *
     * @param masterNode master node name or IP address
     * @return a string containing result value
     * @throws java.lang.Exception
     */
    public String getMapredSite(String masterNode) throws java.lang.Exception {

        VelocityUtil velocity = new VelocityUtil("sg/nus/ece/cnl/veracity/hadoop/velocity/mapred-site.vm");

        velocity.put("masternode", masterNode);

        return velocity.merge().toString();
    }

    /**
     * Render a string containing the start script launched on xen server for
     * start hadoop services on hadoop vm instances.
     *
     * @param hadoopHome hadoop home on hadoop vm instance
     * @param hadoopUser the user run the hadoop services
     * @return script
     * @throws java.lang.Exception
     */
    public String getStartScript(String hadoopHome, String hadoopUser, String execDir) throws java.lang.Exception {

        VelocityUtil velocity = new VelocityUtil("sg/nus/ece/cnl/veracity/hadoop/velocity/start-script.vm");
        
        velocity.put("hadoopHome", hadoopHome);
        velocity.put("hadoopUser", hadoopUser);
        velocity.put("execDir", execDir);

        return velocity.merge().toString();
    }
}
